package pt.ua.helpdesk.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FaqList extends Composite {

	// we need a RecordDef which tells the datatype displayed in the grid

	final VerticalPanel vp = new VerticalPanel();

	private static int user;
	
	public FaqList(int user) {
		this.user = user;
		
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		horizontalPanel.setWidth("100%");
		horizontalPanel.setStyleName("loginPanel");


		horizontalPanel.add(vp);
		vp.setWidth("450");

		final HorizontalPanel hp = new HorizontalPanel();
		vp.add(hp);
		hp.setSpacing(5);
		hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		hp.setWidth("100%");

		final Label listaDeFaqsLabel = new Label("Lista de Faqs");
		hp.add(listaDeFaqsLabel);
		listaDeFaqsLabel.setStyleName("lblSectionTitle");


		if (user == 1)
			ServiceFaq.Util.getInstance().getFaq(new GetAsyncCallback(vp));
		else if (user == 2)
			ServiceFaq.Util.getInstance().getFaqAll(new GetAsyncCallback(vp));
		
	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call
	 * 
	 * This class is the implementation of AsyncCallback for the register
	 * user service
	 * 
	 * @author Amigos do Noddy
	 */	
	  static class GetAsyncCallback implements AsyncCallback {
		  VerticalPanel vp;
		  
		  /**
		   * Class constructor
		   * @param html Widget that can contain arbitrary HTML
		   */
		  public GetAsyncCallback(VerticalPanel vp) {
			  this.vp= vp;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {		  
			  ArrayList ret = (ArrayList)object;
			  
			  ArrayList i = (ArrayList)ret.get(0);
			  ArrayList t = (ArrayList)ret.get(1);
			  ArrayList a = (ArrayList)ret.get(2);
			  ArrayList c = new ArrayList();
			  
			  if (i.size() == 0)
			  {
				  HorizontalPanel hp = new HorizontalPanel();
				  vp.add(hp);
				  hp.setSpacing(5);
				  hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
				  hp.setWidth("100%");

				  Label lblNoFaqs = new Label("Não existem FAQs");
				  hp.add(lblNoFaqs);
				  lblNoFaqs.setStyleName("erro");
				  
				  return;
			  }
			  
			  
			  if (user == 2)
				  c = (ArrayList)ret.get(3);

			  Grid grid = new Grid();
			  if(user == 1)
				  grid.resize(t.size()+1, 4);
			  else if (user == 2)
				  grid.resize(t.size()+1, 6);
			  
			  grid.setStyleName("grid");
			  grid.setBorderWidth(1);
			  grid.setCellSpacing(4);
			  
			  Label lbl1 = new Label();
			  lbl1.setText("#");
			  lbl1.setStyleName("section_title");
			  grid.setWidget(0, 0, lbl1);
			  
			  Label lbl2 = new Label();
			  lbl2.setText("Questão");
			  lbl2.setStyleName("section_title");
			  grid.setWidget(0, 1, lbl2);
			  
			  Label lbl6 = new Label();
			  lbl6.setText("Autor");
			  lbl6.setStyleName("section_title");
			  grid.setWidget(0, 2, lbl6);
			  
			  Label lbl3 = new Label();
			  lbl3.setText("");
			  lbl3.setStyleName("section_title");
			  grid.setWidget(0, 3, lbl3);
			  	  
			  for(int k = 0; k<i.size(); k++)
			  {
				  Label lbl4 = new Label();
				  lbl4.setText(new Integer(k+1).toString());
				  lbl4.setStyleName("label");
				  grid.setWidget(k+1, 0, lbl4);
				  
				  Label lbl5 = new Label();
				  lbl5.setText(t.get(k).toString());
				  lbl5.setStyleName("label");
				  grid.setWidget(k+1, 1, lbl5);
				  
				  Label lbl7 = new Label();
				  lbl7.setText(a.get(k).toString());
				  lbl7.setStyleName("label");
				  grid.setWidget(k+1, 2, lbl7);
				  			  
				  Button btn = new Button();
				  btn.setText("Ver");
				  btn.setStyleName("button");
				  btn.addClickListener(new FaqViewOnClick( new Integer(Integer.parseInt(i.get(k).toString())), new Integer(user)));
				  grid.setWidget(k+1, 3, btn);
				  if (user == 2)
				  {		
					  if (c.get(k).toString().equals("0"))
					  {
						  Button btn1 = new Button();
						  btn1.setText("Aceitar");
						  btn1.setStyleName("button");
						  btn1.addClickListener(new FaqAdmOnClick( new Integer(Integer.parseInt(i.get(k).toString())), new Integer(1)));
						  grid.setWidget(k+1, 4, btn1);
					  }
					  
					  Button btn2 = new Button();
					  btn2.setText("Remover");
					  btn2.setStyleName("button");
					  btn2.addClickListener(new FaqAdmOnClick( new Integer(Integer.parseInt(i.get(k).toString())), new Integer(2)));
					  grid.setWidget(k+1, 5, btn2);
				  }				 				 
			  }
			  
			  vp.add(grid);
			  
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
	}

}
